/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.MyConnectUnit;
import DTO.LoanDiary;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author Quan_Nuce
 */
public class BLoanDiary {
    MyConnectUnit connect;
    public BLoanDiary() {
        this.connect = DAL.DAL.getDAL();
}
    
     public HashSet<LoanDiary>getLoanDiarys(String condition, String OderBy) throws Exception{
        ResultSet result = this.connect.Select("LoanDiary",condition,OderBy);
        HashSet<LoanDiary> loanDiarys = new HashSet<LoanDiary>();
        while(result.next()){
            LoanDiary loanDiary = new LoanDiary();
            loanDiary.setLoanDetailID(result.getInt("LoanDetailID"));
            loanDiary.setDateOfPaid(result.getString("DateOfPaid"));
            loanDiary.setAmountPaid(result.getFloat("AmountPaid"));
            loanDiary.setFineID(result.getInt("FineID"));
         
            loanDiarys.add(loanDiary);
        }
        return loanDiarys;
    }
         
  public HashSet<LoanDiary>getLoanDiarys(String condition) throws Exception{
        return getLoanDiarys(condition,null);
    }
  public HashSet<LoanDiary>getLoanDiarys() throws Exception{
        return getLoanDiarys(null);
    }
   
   public LoanDiary getByID(int id)throws Exception{
        HashSet<LoanDiary>loanDiarys = this.getLoanDiarys("LoanDetailID = "+ id);
        if(loanDiarys.size()>0)
            return loanDiarys.toArray(new LoanDiary[loanDiarys.size()])[0];
        return null;
       
   }
   public void Insert(LoanDiary loanDiary)throws Exception{
        HashMap<String, Object>map = new HashMap<String,Object>();
        map.put("LoanDetailID", loanDiary.getLoanDetailID());
        map.put("DateOfPaid", loanDiary.getDateOfPaid());
        map.put("AmountPaid", loanDiary.getAmountPaid());
        map.put("FineID", loanDiary.getFineID());
       
        this.connect.Insert("LoanDiary", map);
       
   }
   public void Inserts(HashSet<LoanDiary> loanDiarys)throws Exception{
        for(LoanDiary loanDiary:loanDiarys)
            this.Insert(loanDiary);
       
   }
    public void Update(LoanDiary loanDiary)throws Exception{
        HashMap<String, Object>map = new HashMap<String,Object>();
           map.put("DateOfPaid", loanDiary.getDateOfPaid());
        map.put("AmountPaid", loanDiary.getAmountPaid());
        map.put("FineID", loanDiary.getFineID());
        this.connect.Update("LoanDiary", map,"LoanDetailID="+loanDiary.getLoanDetailID());
       
   }
      public void Updates(HashSet<LoanDiary> loanDiarys)throws Exception{
        for(LoanDiary loanDiary:loanDiarys)
            this.Update(loanDiary);
       
   }
   public void Delete(LoanDiary loanDiary) throws Exception{
        this.connect.Delete("LoanDiary", "LoanDetailID="+loanDiary.getLoanDetailID());
    }
       public void Deletes(HashSet<LoanDiary> loanDiarys) throws Exception {
        for(LoanDiary loanDiary:loanDiarys)
            this.Delete(loanDiary);
   }
}
